<?php
class PartCategoriesController extends AppController {

	var $name = 'PartCategories';
	var $components = array('Auth');
	var $helpers = array('Javascript');
	function beforeFilter() {		
		parent::beforeFilter();
		$this->layout="admin";
		$this->Auth->authorize = 'controller';		
	}

    function isAuthorized() {
		$MY_RULE_ID = 1; //Shop
        $allow = $this->requestAction('ruleStaffTypes/allow/'.$MY_RULE_ID);
		//Check xem co quyen su dung khong.
		if ($allow){
			return true;
		}else{
			$this->Session->setFlash(__('Permission denied!',true));
			$this->redirect(array("controller"=>"staffs","action"=>"login","admin"=>false));
			return false;
		}        
        return false;
    }
	function admin_index() {
		$this->set('types',array('1'=>"Part",'2'=>'Consumable'));
		$this->PartCategory->recursive = 0;
		$this->set('partCategories', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid part category', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('partCategory', $this->PartCategory->read(null, $id));
	}

	function admin_add() {
		$this->set('types',array('1'=>"Part",'2'=>'Consumable'));
		if (!empty($this->data)) {
			$this->PartCategory->create();
			if ($this->PartCategory->save($this->data)) {
				$this->Session->setFlash(__('The part category has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The part category could not be saved. Please, try again.', true));
			}
		}
		$this->set("fileupload",date("YmdHis_").rand(100,999));
		$this->set("pathupload","categories");
	}

	function admin_edit($id = null) {
		$this->set('types',array('1'=>"Part",'2'=>'Consumable'));
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid part category', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->PartCategory->save($this->data)) {
				$this->Session->setFlash(__('The part category has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The part category could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->PartCategory->read(null, $id);
		}
		$filename=$this->data["PartCategory"]["photo"];
		$extension=substr($filename,strrpos($filename,"."),strlen($filename));
		$filename=substr($filename,0,strrpos($filename,"."));		
		$this->set("extension",$extension);
		$this->set("fileupload",((empty($filename))?(date("YmdHis_").rand(100,999)):$filename));
		$this->set("pathupload","categories");
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for part category', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->PartCategory->delete($id)) {
			$this->Session->setFlash(__('Part category deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Part category was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>